package com.xzx.mybatis;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xzx.mybatis.mapper.DeptMapper;
import com.xzx.mybatis.mapper.EmployeeMapper;
import com.xzx.mybatis.po.DeptPO;
import com.xzx.mybatis.po.EmployeePO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

/**
 * @author xinzhixuan
 * @version 1.0
 * @date 2025/6/21 12:40
 */
@Slf4j
public class Main {

    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void getSqlSessionFactory() throws IOException {
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
    }

    @Test
    public void test() {
        // true表示自动提交
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        Page<EmployeePO> page = PageHelper.offsetPage(2, 2);
        List<EmployeePO> list = mapper.getList();
        // Page{count=true, pageNum=2, pageSize=2, startRow=2, endRow=4, total=6, pages=3, reasonable=false, pageSizeZero=false}[EmployeePO(id=5, lastName=发财2, email=456@qq.com, gender=0, dept=null), EmployeePO(id=6, lastName=发财2, email=<EMAIL>, gender=1, dept=null)]
        System.out.println(page);
        System.out.println(page.getTotal());
        System.out.println(page.getResult() == page); //true
        System.out.println(list == page); //true
        sqlSession.close();
    }

    @Test
    public void testBatchInsert() {
        // true表示自动提交
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        long start = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            EmployeePO employeePO = new EmployeePO();
            employeePO.setLastName("发财" + i);
            employeePO.setEmail("123" + i + "@qq.com");
            employeePO.setGender(i % 2);
            //  ==> Parameters: 发财99999(String), 12399999@qq.com(String), 1(Integer) 只会攒批次，并不会发sql
            mapper.insert(employeePO);
        }
        sqlSession.commit();
        sqlSession.close();
        // 34902
        System.out.println("cost:" + (System.currentTimeMillis() - start));
    }

    @Test
    public void testEnum() {
        // true表示自动提交
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        EmployeePO employeePO = new EmployeePO();
        employeePO.setLastName("发财");
        employeePO.setEmail("123@qq.com");
        employeePO.setGender(1);
        employeePO.setStatus(Status.ONLINE);
        mapper.insert(employeePO);
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void testGetEnum() {
        // true表示自动提交
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        EmployeePO po = mapper.getById(3L);
        System.out.println(po);
        sqlSession.close();
    }
}